#include "stdafx.h"
#include "LgSpriteGemCarrier.h"
#include "LgSpriteProperty.h"
LgSpriteGemCarrier::LgSpriteGemCarrier(int nTime,D3DXVECTOR2 p1,D3DXVECTOR2 p2):
m_nTime(nTime),m_pCargo(NULL)
{
	m_bShow=false;
	m_eClassName=eLgSpriteGemCarrier;
	m_StartPos=p1;
	m_EndPos=p2;
	m_DropPos=(p1+p2)/2.0f;
	m_vPosition=m_StartPos;
	m_vecHitRect.clear();
	if(0!=nTime){
		m_fSpeedx=(m_EndPos.x-m_StartPos.x)/nTime;
		m_fSpeedy=(m_EndPos.y-m_StartPos.y)/nTime;
	}else{
		m_fSpeedx=0;
		m_fSpeedy=0;
	}
}

LgSpriteGemCarrier::~LgSpriteGemCarrier()
{

}

void LgSpriteGemCarrier::Update(DWORD dwCurrentTime)
{
	if (!m_bShow&&!m_listJob.empty()){
		m_bShow=true;
		m_fAnimationTimer=0;
		m_vPosition=m_StartPos;
		Cargo_t st;
		st=m_listJob.front();
		m_pCargo=st.pCargo;
		m_DropPos=st.DropPos;
		m_listJob.pop();
	}
	if(!m_bShow) return;

	DWORD nElpasedTime=0;
	if (-1!=m_nLastTime){
		nElpasedTime=dwCurrentTime-m_nLastTime;
	}

	if( m_fAnimationTimer > m_nTime*1000 ) {
		m_fAnimationTimer=0;
		m_vPosition=m_EndPos;
		m_bShow=false;
	}else{
		m_vPosition.x += m_fSpeedx * nElpasedTime/1000;
		m_vPosition.y += m_fSpeedy * nElpasedTime/1000;
		if (fabs(m_vPosition.x-m_DropPos.x)<3 &&
			fabs(m_vPosition.y-m_DropPos.y)<3){
			if(m_pCargo){
				m_pCargo->PopUp(m_DropPos.x,m_DropPos.x,m_DropPos.y);
			}
		}
	}

	DynamicLgSprite::Update(dwCurrentTime);
}

void LgSpriteGemCarrier::FlyOut(LgSpriteProperty *pCargo, D3DXVECTOR2 pos)
{
	Cargo_t st;
  	st.pCargo=pCargo;
	st.DropPos=pos;
	m_listJob.push(st);
}
